package com.dehui.traders.kchart;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.SparseArray;

import com.dehui.traders.R;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;

public class XVolumeManager{

    private static XVolumeManager volumeManager;
    private MyCombinedChartX mChartVolume;
    private Context mContext;
    private DataParse mData;
    private boolean isDragChart;
    private MyXAxis xAxisVolume;
    private MyYAxis axisLeftVolume;
    private MyYAxis axisRightVolume;
    protected SparseArray<String> stringSparseArray;

    public static XVolumeManager getInstange(){
        if (volumeManager == null)
            volumeManager = new XVolumeManager();
        return volumeManager;
    }

    public void initVolume(Context context, MyCombinedChartX chart, DataParse mData,boolean isDragChart){
        mChartVolume = chart;
        mContext = context;
        this.mData = mData;
        this.isDragChart = isDragChart;
        stringSparseArray = setXLabels();
        initChartVolume();
        setShowLabels(stringSparseArray);
        initChartVolumeData(mChartVolume);
    }

    private SparseArray<String> setXLabels() {
        SparseArray<String> xLabels = new SparseArray<>();
        xLabels.put(0, "09:30");
        xLabels.put(60, "10:30");
        xLabels.put(121, "11:30/13:00");
        xLabels.put(182, "14:00");
        xLabels.put(241, "15:00");
        return xLabels;
    }

    private void initChartVolume() {
        mChartVolume.setScaleEnabled(false);//启用图表缩放事件
        mChartVolume.setDrawBorders(true);//是否绘制边线
        mChartVolume.setBorderWidth(1);//边线宽度，单位dp
        mChartVolume.setDragEnabled(isDragChart);//启用图表拖拽事件
        mChartVolume.setScaleYEnabled(false);//启用Y轴上的缩放
        mChartVolume.setBorderColor(mContext.getResources().getColor(R.color.border_color));//边线颜色
        mChartVolume.setDescription("");//右下角对图表的描述信息
        mChartVolume.setHardwareAccelerationEnabled(true);//是否开启硬件加速
        mChartVolume.setMinOffset(0f);//设置上下内边距
        mChartVolume.setExtraOffsets(0f, 0f, 0f, 0f);//图标周围格额外的偏移量

        Legend lineChartLegend = mChartVolume.getLegend();
        lineChartLegend.setEnabled(false);//是否绘制 Legend 图例

        //x轴
        xAxisVolume = mChartVolume.getXAxis();
        xAxisVolume.setEnabled(false);//是否绘制X轴的数据
//        xAxisVolume.setDrawLabels(false);
//        xAxisVolume.setDrawAxisLine(false);
//        xAxisVolume.setDrawGridLines(false);
//        xAxisVolume.enableGridDashedLine(10f, 10f, 0f);//绘制成虚线，只有在关闭硬件加速的情况下才能使用

        //左边y
        axisLeftVolume = mChartVolume.getAxisLeft();
        axisLeftVolume.setAxisMinValue(0);//设置Y轴坐标最小为多少
        axisLeftVolume.setShowOnlyMinMax(true);//参考上面
        axisLeftVolume.setDrawLabels(false);//参考上面
        axisLeftVolume.setDrawGridLines(false);//参考上面
        /*轴不显示 避免和border冲突*/
        axisLeftVolume.setDrawAxisLine(false);//参考上面

        //右边y
        axisRightVolume = mChartVolume.getAxisRight();
        axisRightVolume.setAxisMinValue(0);//参考上面
        axisRightVolume.setShowOnlyMinMax(true);//参考上面
        axisRightVolume.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);//参考上面
        axisRightVolume.setDrawLabels(true);//参考上面
        axisRightVolume.setDrawGridLines(true);//参考上面
        axisRightVolume.enableGridDashedLine(10f, 10f, 0f);//参考上面
        axisRightVolume.setDrawAxisLine(false);//参考上面

        //y轴样式
        this.axisRightVolume.setValueFormatter(new YAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, YAxis yAxis) {
                DecimalFormat mFormat = new DecimalFormat("#0.00");
                return mFormat.format(value);
            }
        });
    }


    public void setShowLabels(SparseArray<String> labels) {
        xAxisVolume.setXLabels(labels);
    }

    private void initChartVolumeData(MyCombinedChartX combinedChartX) {
        setMarkerView(mData, combinedChartX);
        combinedChartX.getAxisLeft().setAxisMaxValue(mData.getVolmax()); /*单位*/
        combinedChartX.getAxisRight().setAxisMaxValue(mData.getVolmax());

        ArrayList<Entry> lineJJEntries = new ArrayList<>();
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        for (int i = 0, j = 0; i < mData.getDatas().size(); i++, j++) {
            MinutesBean t = mData.getDatas().get(j);

            if (t == null) {
                barEntries.add(new BarEntry(Float.NaN, i));
                continue;
            }
            if (!TextUtils.isEmpty(stringSparseArray.get(i)) &&
                    stringSparseArray.get(i).contains("/")) {
                i++;
            }
            lineJJEntries.add(new Entry(0, i));
            BarEntry e = new BarEntry(mData.getDatas().get(i).cjnum, i);
            int color = t.color;
            if (color == 1){
                e.setClose(2);
                e.setOpen(1);
            }else{
                e.setClose(1);
                e.setOpen(2);
            }
            barEntries.add(e);
        }
        BarDataSet barDataSet = new BarDataSet(barEntries, "成交量");
//        barDataSet.setBarSpacePercent(20); //bar空隙，可以控制树状图的大小，空隙越大，树状图越窄
        barDataSet.setHighLightColor(mContext.getResources().getColor(R.color.marker_line_bg));// 设置点击某个点时，横竖两条线的颜色，就是高亮线的颜色
        barDataSet.setHighLightAlpha(255);//设置高亮线的透明度
        barDataSet.setDrawValues(false);//是否在线上绘制数值
        barDataSet.setHighlightEnabled(true);//是否启用高亮线
        barDataSet.setColor(mContext.getResources().getColor(R.color.increasing_color));//设置树状图颜色
        List<Integer> list = new ArrayList<>();
        list.add(mContext.getResources().getColor(R.color.increasing_color));
        list.add(mContext.getResources().getColor(R.color.decreasing_color));
        barDataSet.setColors(list);//可以给树状图设置多个颜色，判断条件在BarChartRenderer 类的140行以下修改了判断条件
        barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);//设置这些值对应哪条轴
        BarData barData = new BarData(getMinutesCount(), barDataSet);

        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(setLine(2, getMinutesCount(), lineJJEntries));
        /*注老版本LineData参数可以为空，最新版本会报错，修改进入ChartData加入if判断*/
        LineData lineData = new LineData(getMinutesCount(), sets);
        lineData.setHighlightEnabled(false);

        CombinedData combinedData = new CombinedData(getMinutesCount());
        combinedData.setData(barData);
        combinedData.setData(lineData);
        combinedChartX.setData(combinedData);

        combinedChartX.invalidate();
    }

    private void setMarkerView(DataParse mData, MyCombinedChartX combinedChart) {
        MyLeftMarkerView leftMarkerView = new MyLeftMarkerView(mContext, R.layout.mymarkerview);
        MyHMarkerView hMarkerView = new MyHMarkerView(mContext, R.layout.mymarkerview_line);
        combinedChart.setMarker(leftMarkerView, hMarkerView, mData);
    }

    public String[] getMinutesCount() {
        return new String[242];
    }

    @NonNull
    private LineDataSet setLine(int type, String[] xVals, ArrayList<Entry> lineEntries) {
        LineDataSet lineDataSetMa = new LineDataSet(lineEntries, "ma" + type);
        lineDataSetMa.setHighlightEnabled(false);
        lineDataSetMa.setDrawValues(false);
        if (type == 0) {
            lineDataSetMa.setAxisDependency(YAxis.AxisDependency.LEFT);
            lineDataSetMa.setColor(mContext.getResources().getColor(R.color.minute_blue));
        } else if (type == 1) {
            lineDataSetMa.setAxisDependency(YAxis.AxisDependency.RIGHT);
            lineDataSetMa.setColor(mContext.getResources().getColor(R.color.minute_yellow));
        } else {
            lineDataSetMa.setAxisDependency(YAxis.AxisDependency.RIGHT);
            lineDataSetMa.setColor(mContext.getResources().getColor(R.color.transparent));
        }
        lineDataSetMa.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSetMa.setLineWidth(1f);

        lineDataSetMa.setDrawCircles(false);
        lineDataSetMa.setAxisDependency(YAxis.AxisDependency.LEFT);

        return lineDataSetMa;
    }

}
